package com.itheima.health.service.impl;

import com.itheima.health.dao.OrderSettingMapper;
import com.itheima.health.exception.BusinessException;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;


@DubboService
@Slf4j
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;
    /**
     * @Author: JASON
     * @Date: 10:56 2021/1/9
     * @Parms [year, month]
     * @ReturnType: java.util.List<com.itheima.health.pojo.OrderSetting>
     * @Description:
    */
    @Override
    public List<OrderSetting> findByMonth(Integer year, Integer month) {
        log.info("预约设置-查询预约数据year：{},Month:{}",year,month);
        //sql参数拼接 开始时间和结束时间
        String beginDate = String.format("%d-%d-1", year, month);
        String endDate = String.format("%d-%d-31", year, month);
        log.info("预约设置-查询预约数据begindata:{},enddate{}",beginDate,endDate);
        Example example = new Example(OrderSetting.class);
        Example.Criteria criteria = example.createCriteria();
        //设置范围查询条件
        criteria.andGreaterThanOrEqualTo("orderDate", beginDate);
        criteria.andLessThanOrEqualTo("orderDate", endDate);
        List<OrderSetting> orderSettingList = orderSettingMapper.selectByExample(example);
        return orderSettingList;
    }

    /**
     * @Author: JASON
     * @Date: 11:50 2021/1/9
     * @Parms [orderSetting]
     * @ReturnType: void
     * @Description: 设置预约人数
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editNumberByDate(OrderSetting orderSetting) {
        log.info("Service设置预约人数"+orderSetting);
        //存在则修改
        Example example = new Example(OrderSetting.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("orderDate", orderSetting.getOrderDate());
        //根据条件更新记录
        Integer count = orderSettingMapper.findReservationsByDate(orderSetting.getOrderDate());
        if(orderSetting.getNumber()<count){
            throw new BusinessException("设置人数少于已预约人数设置不成功");
        }
        int i = orderSettingMapper.updateByExampleSelective(orderSetting, example);
        if(i==0){
            //没有改orderDate中的数据
            orderSettingMapper.insertSelective(orderSetting);
        }
    }

    /**
     * @Author: JASON
     * @Date: 15:29 2021/1/9
     * @Parms [orderSettings]
     * @ReturnType: void
     * @Description: 批量添加数据
    */
    @Override
    public void addAll(List<OrderSetting> orderSettings) {
        log.info("批量插入数据"+orderSettings);
        Example example = new Example(OrderSetting.class);
        Example.Criteria criteria = example.createCriteria();
        for (OrderSetting orderSetting : orderSettings) {
            criteria.andEqualTo("orderDate", orderSetting.getOrderDate());
            //先根据日期修改 如果修改不存在数据
            int i = orderSettingMapper.updateByExampleSelective(orderSetting, example);
            if(i==0){
                //然后执行添加
                orderSettingMapper.insertSelective(orderSetting);
            }
        }

    }
}
